const app = getApp()
//转换日期格式
const formatDate = function (dateValue, format) {
  var date = new Date(dateValue);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  var minute = date.getMinutes();
  minute = minute < 10 ? ('0' + minute) : minute;
  let time = y + '/' + m + '/' + d
  if (format == 'MM-DD') {
    time = m + '-' + d
  }
  if (format == 'MM') {
    time = m
  }
  if (format == 'YY-MM') {
    time = y + '/' + m
  }
  if (format == 'YY-MM-DD') {
    time = y + '-' + m + '-' + d
  }
  if (format == 'YY-MM-DD HH:mm') {
    time = y + '/' + m + '/' + d + ' ' + h + ':' + minute
  }
  return time
}
/* 时间yyyy-MM-dd HH:mm:ss转为时间戳 */
const timeToTimestamp = (time) => {
  let timestamp = Date.parse(new Date(time).toString());
  return timestamp;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {

    current: 1,
    autoplay: true,
    duration: 500,
    interval: 5000,
    swiperList:[
      {
        value: `https://t7.baidu.com/it/u=2529476510,3041785782&fm=193&f=GIF`,
        ariaLabel: '图片1',
      },
      {
        value: `https://t7.baidu.com/it/u=2529476510,3041785782&fm=193&f=GIF`,
        ariaLabel: '图片2',
      },
      {
        value: `https://t7.baidu.com/it/u=2529476510,3041785782&fm=193&f=GIF`,
        ariaLabel: '图片3',
      }
    ],
    navigation: { type: '', showControls: true },


    dateArr: [],
    selectdateValue: formatDate(new Date(), 'YY-MM-DD'),
    fishRangeInfo:{
      name:'永顺园垂钓俱乐部',
      location:'北京市 通州 聊城西三路',
      distance:'137.89'
    },
    itemInfo:{},
    infoDetail:{},
    infoList: [{
      name: '正钓 鲤鱼 撒鱼 15000斤',
      tags: ['活动', '禁止换位', '可用优惠券'],
      anytimeposition: '5小时',
      playmethod: '正约',
      position: '抽顺序号-选钓位',
      price: '6元/斤',
      current_price:'432',
      original_price:'480',
      group_list: [{
          type: '2',
          detail: '差2人成团',
          desc: '0/2',
          price: 420
        },
        {
          type: '3',
          detail: '差2人成团',
          desc: '1/3',
          price: 400
        },
        {
          type: '4',
          detail: '差2人成团',
          desc: '2/4',
          price: 400
        },
        {
          type: '4',
          detail: '差2人成团',
          desc: '2/4',
          price: 400
        },
        {
          type: '4',
          detail: '差2人成团',
          desc: '2/4',
          price: 400
        }
      ]
    },
    {
      name: '正钓 鲤鱼 撒鱼 15000斤',
      tags: ['活动', '禁止换位', '可用优惠券'],
      anytimeposition: '5小时',
      playmethod: '正约',
      position: '抽顺序号-选钓位',
      price: '6元/斤',
      current_price:'432',
      original_price:'480',
      group_list: [{
          type: '2',
          detail: '差2人成团',
          desc: '0/2',
          price: 420
        },
        {
          type: '3',
          detail: '差2人成团',
          desc: '1/3',
          price: 400
        },
        {
          type: '4',
          detail: '差2人成团',
          desc: '2/4',
          price: 400
        },
        {
          type: '4',
          detail: '差2人成团',
          desc: '2/4',
          price: 400
        },
        {
          type: '4',
          detail: '差2人成团',
          desc: '2/4',
          price: 400
        }
      ]
		},
		{
      name: '正钓 鲤鱼 撒鱼 15000斤',
      tags: ['活动', '禁止换位', '可用优惠券'],
      anytimeposition: '5小时',
      playmethod: '正约',
      position: '抽顺序号-选钓位',
      price: '6元/斤',
      current_price:'432',
      original_price:'480',
      group_list: [{
          type: '2',
          detail: '差2人成团',
          desc: '0/2',
          price: 420
        },
        {
          type: '3',
          detail: '差2人成团',
          desc: '1/3',
          price: 400
        },
        {
          type: '4',
          detail: '差2人成团',
          desc: '2/4',
          price: 400
        },
        {
          type: '4',
          detail: '差2人成团',
          desc: '2/4',
          price: 400
        },
        {
          type: '4',
          detail: '差2人成团',
          desc: '2/4',
          price: 400
        }
      ]
    }
  ],


  },
  //计算未来15天的
  getWeekDate() {
    let dateArr = []
    var weekDay = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
    for (let index = 0; index < 7; index++) {
      let obj = {
        dateName: '',
        dateLabel:'',
        dateValue: '',
      }
      let today = new Date()
      today = timeToTimestamp(today) + index * 86400000
      if (index == 0) {
        obj.dateName = '今天'
      } else if (index == 1) {
        obj.dateName = '明天'
      } else if (index == 2) {
        obj.dateName = '后天'
      } else {
        obj.dateName = weekDay[new Date(today).getDay()]
      }
      obj.dateValue = formatDate(today, 'YY-MM-DD')
      obj.dateLabel = formatDate(today, 'MM-DD')
      dateArr.push(obj)
    }
    this.setData({
      dateArr: dateArr
    })
  },

  clickDate(e) {
    this.setData({
      selectdateValue: e.currentTarget.dataset.name,
    })
    this.getDetail()
  },
    // 查看钓场详情
    viewFishingIntroduction(e){
  
      let info=JSON.stringify(this.data.infoDetail)
      console.log(this.data.infoDetail);
      wx.navigateTo({
        url: `/pages/fishingRangeIntroduction/fishingRangeIntroduction?info=${info}`
      });
    },

  // 去报名
  tosignup(e){
    let info=e.currentTarget.dataset.item
    info.fish_place_name=this.data.infoDetail.fish_place_name
    info.selectdateValue=this.data.selectdateValue
    info.mobile_phone=this.data.itemInfo.mobile_phone
    info=JSON.stringify(info)
    wx.navigateTo({
      url: `/pages/submitOrder/submitOrder?info=${info}&groupNum=`
    });
  },

  //去拼团
  toGroupBuy(e){
    let groupNum=e.currentTarget.dataset.groupname
    let info=e.currentTarget.dataset.item
    info.fish_place_name=this.data.infoDetail.fish_place_name
    info.selectdateValue=this.data.selectdateValue
    info.mobile_phone=this.data.itemInfo.mobile_phone
    info=JSON.stringify(info)
    wx.navigateTo({
      url: `/pages/submitOrder/submitOrder?info=${info}&groupNum=${groupNum}`
    });
  },
  //关注和取消关注
  clickLove(e){
    let data={
      favorites_state:e.currentTarget.dataset.love,
      fish_place_id:this.data.itemInfo.fish_place_id
    }
    app.request('post',  `/user/editUserFavorites?mobile_phone=${this.data.itemInfo.mobile_phone}`, data, res =>{
      this.getDetail()
    })
  },

  //获取详情
  getDetail(){
    let data={
      latitude:this.data.itemInfo.latitude,
      longitude:this.data.itemInfo.longitude,
      time:this.data.selectdateValue,
      mobile_phone:this.data.itemInfo.mobile_phone,
      fish_place_id:this.data.itemInfo.fish_place_id
    }
    app.request('get', '/fishnews/getPlaceNewsInfo', data, res =>{
      this.setData({infoDetail: res.data.data.PlaceNewsInfo[0],infoList:res.data.data.PlaceNewsInfo[0].fishnews})
    })
  
  },
  tellphone() {
    wx.makePhoneCall({
      phoneNumber: this.data.infoDetail.fish_place_mobile
    })
  },
  toMap(){
    // wx.navigateTo({
    //   url: `/pages/map/map`
    // });
    wx.openLocation({
      name:this.data.itemInfo.fish_place_name,
      address:this.data.infoDetail.province+this.data.infoDetail.city+this.data.infoDetail.district+this.data.infoDetail.detail_address+' 距离您'+this.data.infoDetail.distance+'KM',
      latitude:parseFloat(this.data.infoDetail.latitude) ,
      longitude:parseFloat(this.data.infoDetail.longitude),
      scale: 18
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let itemJson=JSON.parse(options.itemJson)
    console.log(itemJson);
    this.setData({
      itemInfo:itemJson
    })
    this.getWeekDate()
    this.getDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})